package com.wudi.estate.service.impl.admin;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wudi.estate.bean.admin.Permission;
import com.wudi.estate.bean.admin.Role;
import com.wudi.estate.mapper.admin.PermissionMapper;
import com.wudi.estate.mapper.admin.RoleMapper;
import com.wudi.estate.service.admin.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 丰哥
 * 2022/10/12
 * 角色业务层的实现
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Map<String, Object>> selectRoleAndPermissions() throws Exception {

        List<Map<String, Object>> rolePermissions =
                new ArrayList<>();
        // ① 角色表：查询所有的角色
        List<Role> roles = roleMapper.selectAll();
        for (Role role : roles) {
            Map<String, Object> rolePermission = new HashMap<>();
            Permission permission = permissionMapper.selectById(role.getPid());
            rolePermission.put("role",role);
            rolePermission.put("permission",permission);
            rolePermissions.add(rolePermission);
        }
//        for (Role role : roles) {
//
//            Map<String, Object> rolePermission = new HashMap<>();
//            rolePermission.put("rid", role.getId());
//            rolePermission.put("rname", role.getName());
//
//            // ② 角色权限表： 通过角色的id，查询对于的所有的权限id
//            List<Long> rolePermissionIds = roleMapper.selectPIdsByRId(role.getId());
//            List<String> permissonNames = new ArrayList<>();
//            for (Long rolePermissionId : rolePermissionIds) {
//                // ③ 权限表： 通过权限的id，查询权限名称
//                String permissionName = permissionMapper.selectNameById(rolePermissionId);
//                permissonNames.add(permissionName);
//            }
//            rolePermission.put("permissonNames", permissonNames);
//
//            rolePermissions.add(rolePermission);
//        }

        // ④ 数组组装List<Map<String, Object>>

        return rolePermissions;
    }

    @Override
    public void insertRole(String rname, long[] pids) throws Exception {

    }

    @Override
    public List<Map<String,Object>> selectAll() throws Exception {
        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        List<Map<String,Object>> lists = new ArrayList<>();
        Map<String,Object> maps = new HashMap<>();
        List<Role> roles = roleMapper.selectList(roleQueryWrapper);
        List<Permission> permissions = permissionMapper.selectList(null);
        maps.put("roles",roles);
        maps.put("permissions",permissions);
        lists.add(maps);
        roleQueryWrapper.select("distinct name");
        return lists;
    }

    @Override
    public Role selectById(int id) throws Exception {
        return roleMapper.selectById(id);
    }
}
